package com.example.fitapp.activity.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.transition.Transition;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.fitapp.activity.R;
import com.example.fitapp.activity.activity.HomeDetail;
import com.example.fitapp.activity.activity.PushActivity;
import com.example.fitapp.activity.bean.DynamicInfo;
import com.example.fitapp.activity.bean.MySerializable;
import com.example.fitapp.activity.utils.BitMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class HomeDataAdapter extends RecyclerView.Adapter<HomeDataAdapter.ViewHolder> {
    private List<DynamicInfo.DataBean> dataBeans;
    private Context context;


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView userName;
        ImageView user_header;
        TextView common;
        TextView title;
        View itemcart;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            userName = (TextView) itemView.findViewById(R.id.user_name);
            common = (TextView) itemView.findViewById(R.id.common);
            title = (TextView) itemView.findViewById(R.id.title);
            user_header = (ImageView) itemView.findViewById(R.id.user_header);
            itemcart = itemView;
        }
    }

    public HomeDataAdapter(List<DynamicInfo.DataBean> dataBeans) {
        this.dataBeans = dataBeans;
    }

    private Map<String, Float> imageScaleMap = new HashMap<>();

    public void initImageScale() {
            Log.i("picList", dataBeans.size() + "");
            for (DynamicInfo.DataBean dataBean : dataBeans) {
                if(dataBeans.size() > 0){
                    final String url = dataBean.getImages().get(0).getImage();
                    if (imageScaleMap.get(url) == null) {

                        Glide.with(context).load(url).into(new SimpleTarget<Drawable>() {
                            @Override
                            public void onResourceReady(Drawable resource, com.bumptech.glide.request.transition.Transition<? super Drawable> transition) {
                                float scale = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight();
                                imageScaleMap.put(url, scale);
                                Log.i(url, imageScaleMap.get(url).toString());
                                notifyDataSetChanged();
                            }
                        });
                    } else {
                        Log.i(url, "!null");
                        notifyDataSetChanged();
                    }
                }
            }
    }

//    public String JSONToken

    public void setContext(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_layout,parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.itemcart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();

                Log.i("Data","" + position);

                MySerializable mySerializable = new MySerializable();

                mySerializable.setName(dataBeans.get(position).getUserInfo().getNickname());

                mySerializable.setRichText(dataBeans.get(position).getRichText());
                mySerializable.setImageList(dataBeans.get(position).getImages());
                mySerializable.setUser_header(dataBeans.get(position).getUserInfo().getHeader());
                Intent intent = new Intent(context, HomeDetail.class);
                intent.putExtra("DetailItem",mySerializable);
                context.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.image.getLayoutParams();

        DynamicInfo.DataBean dataBean =  dataBeans.get(position);
        Object object = dataBean.getUserInfo().getNickname();

        if (dataBean.getUserInfo().getNickname()  != ""){
            holder.userName.setText(dataBean.getUserInfo().getNickname());
        }else {
            holder.userName.setText("匿名");
        }
        if (dataBean.getUserInfo().getHeader() != ""){
            Glide.with(context).load(dataBean.getUserInfo().getHeader()).into(holder.user_header);
        }

        holder.common.setText(dataBean.getRichText());
        holder.title.setText(dataBean.getTitle());

        if (dataBean.getImages().size()<= 0) return;

        final String data = dataBean.getImages().get(0).getImage();
//        HttpUtil.getBitmap(data, new HttpUtil.HttpGetBitmapCallBack() {
//            @Override
//            public void onFailed() {
//
//            }
//
//            @Override
//            public void onSuccess(Bitmap bitmap) {
//                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.image.getLayoutParams();
//                layoutParams.width = BitMap.getScreenWidth((Activity) context) / 2 - BitMap.px2dip(context, 8);
//
//                if (imageScaleMap.get(dataBeans.get(position).getImages().get(0).getImage()) != null) {
//                    layoutParams.height = (int) (layoutParams.width / imageScaleMap.get(dataBeans.get(position).getImages().get(0).getImage()));
//                }
//                Log.i("bitmap","" +  layoutParams.width );
//            }
//        });


        Glide.with(context).load(data).transition(new DrawableTransitionOptions().crossFade()).into(holder.image);

    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }


}
